home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte19 / ex5.c < prev    next >
C/C++ Source or Header  |  1995-06-03  |  4KB  |  122 lines

  1. #include <windows.h>
  2. #include <winreg.h>
  3. #include <genstub.c>
  4.  
  5. // Menu options.
  6. #define IDM_SAVE       400     // Create a Hive.
  7. #define IDM_RESTORE    401     // Restore the Hive.  Not supported in Win 95.
  8. #define IDM_LOAD       402     // Load the Hive.
  9. #define IDM_UNLOAD     403     // Unload the Hive.
  10.  
  11. LRESULT FAR PASCAL WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  12. {
  13.    switch ( uMsg )
  14.    {
  15.       case WM_COMMAND:
  16.       {
  17.          char szErrMsg[128]; // Buffer for message to print in message boxes
  18.          HKEY hKey;          // Handle of key.
  19.          LRESULT lResult;    // Tests function return values.
  20.          switch ( wParam )
  21.          {
  22.             case IDM_SAVE:
  23.             {
  24.                DWORD dwDisp;
  25.  
  26.                // Build the contents of the KeyRoot and its subkeys.
  27.                RegCreateKeyEx( HKEY_CLASSES_ROOT, "KeyRoot\\Test\\Test1", 0, NULL,
  28.                                REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  29.                                &hKey, &dwDisp );
  30.                RegSetValueEx( hKey, "Value1", 0, REG_SZ, "ABC", 14 );
  31.                RegSetValueEx( hKey, "Value2", 0, REG_SZ, "BCD", 14 );
  32.                RegCloseKey( hKey );
  33.                RegCreateKeyEx( HKEY_CLASSES_ROOT, "KeyRoot\\Test\\Test2", 0, NULL,
  34.                                REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  35.                                &hKey, &dwDisp );
  36.                RegSetValueEx( hKey, "Value1", 0, REG_SZ, "ZYX", 14 );
  37.                RegCloseKey( hKey );
  38.  
  39.                // Save key, "KeyRoot", in hive, "TheHive".
  40.                RegOpenKeyEx( HKEY_CLASSES_ROOT, "KeyRoot", 0, KEY_ALL_ACCESS, &hKey );
  41.                RegSaveKey( hKey, "TheHive", NULL );
  42.                RegCloseKey( hKey );
  43.             }
  44.             break;
  45.             case IDM_LOAD:
  46.             {
  47.                lResult = RegLoadKey( HKEY_LOCAL_MACHINE, "KeyRoot", "TheHive" );
  48.                if ( GetLastError() == ERROR_SUCCESS )
  49.                   MessageBox( hWnd, "TheHive was loaded into HKEY_LOCAL_MACHINE",
  50.                               "MESSAGE", MB_OK );
  51.                else {
  52.                    wsprintf( szErrMsg, "Error %d occurred", GetLastError( ) );
  53.                    MessageBox( hWnd, szErrMsg, "ERROR", MB_OK );
  54.                }
  55.             }
  56.             break;
  57.             case IDM_UNLOAD:
  58.             {
  59.                lResult = RegUnLoadKey( HKEY_LOCAL_MACHINE, "KeyRoot" );
  60.                if ( GetLastError() == ERROR_SUCCESS )
  61.                   MessageBox( hWnd, "TheHive Was Unloaded",
  62.                               "MESSAGE", MB_OK );
  63.                else {
  64.                    wsprintf( szErrMsg, "Error %d occurred", GetLastError( ) );
  65.                    MessageBox( hWnd, szErrMsg, "ERROR", MB_OK );
  66.                }
  67.             }
  68.             break;
  69.             case IDM_RESTORE:
  70.             {
  71.                DWORD dwDisposition = 0;
  72.  
  73.                RegCreateKeyEx( HKEY_LOCAL_MACHINE, "KeyRoot", 0, "",
  74.                                REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
  75.                                NULL, &hKey, &dwDisposition );
  76.  
  77.                RegRestoreKey( hKey, "TheHive", 0 );
  78.                if ( GetLastError() != ERROR_SUCCESS )
  79.                {
  80.                   wsprintf( szErrMsg, "Error %d occurred", GetLastError( ) );
  81.                   MessageBox( hWnd, szErrMsg, "ERROR", MB_OK );
  82.                   RegDeleteKey( HKEY_LOCAL_MACHINE, hKey );
  83.                }
  84.                else
  85.                   MessageBox( hWnd, "The Hive was restored", "MESSAGE", MB_OK );
  86.  
  87.                RegCloseKey( hKey );
  88.             }
  89.             break;
  90.             case IDM_EXIT:
  91.                DestroyWindow( hWnd );
  92.                break;
  93.          }
  94.       }
  95.       break;
  96.       case WM_DESTROY:
  97.          PostQuitMessage( 0 );
  98.       break;
  99.       default:            // default windows message processing
  100.          return DefWindowProc( hWnd, uMsg, wParam, lParam );
  101.    }
  102.    return( 0L );
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.